package com.venue.hjt.venueclass.fragment

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.util.Log
import android.view.View
import com.venue.hjt.venueclass.R
import com.venue.hjt.venueclass.adapter.RecordAdapter
import com.venue.hjt.venueclass.db.CardDao
import com.venue.hjt.venueclass.db.VenueBooKing
import com.venue.hjt.venueclass.ui.VenueSignActivity
import com.venue.hjt.venueclass.utlis.ClassCalendar
import com.venue.hjt.venueclass.utlis.Constant
import kotlinx.android.synthetic.main.fragment_record.*

/**
 * Created by xqx on 2018/3/14.
 *
 */
class TabRecordFragment :BaseFragment() {
    override fun initView(view: View?) {
    }

    override fun getLayoutResource(): Int {
        return R.layout.fragment_record
    }
    val list=ArrayList<VenueBooKing>()
    var adapter:RecordAdapter?=null
    var record:RecordReceiver?=null
    override fun loadData() {
        record=RecordReceiver()
        activity.registerReceiver(record,filter())
        list+=CardDao.queryVenueBooKing()
        adapter= RecordAdapter(activity,list)
        lv_record.adapter=adapter
        lv_record.setOnItemClickListener { adapterView, view, i, l ->
            val time = getTime(list[i].start_time, list[i].end_time)
            val intent= Intent(activity,VenueSignActivity::class.java)
            intent.putExtra("id",list[i].id)
            intent.putExtra("groupname",list[i].groupname)
            intent.putExtra("time",list[i].start_time+"～"+list[i].end_time)
            intent.putExtra("user_score",list[i].user_score)
            intent.putExtra("booker",list[i].booker)
            intent.putExtra("manager_score",list[i].manager_score)
            intent.putExtra("type",time)
            startActivity(intent)
        }
    }
    /**
     * 广播类
     */
    inner class RecordReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            val action = intent.action
            when(action){
                Constant.RECORD->{
                    Log.e("ATG","执行删除3")
                    list.clear()
                    list+=CardDao.queryVenueBooKing()
                    adapter!!.notifyDataSetChanged()
                }
            }

        }
    }
    /**
     * @return IntentFilter 广播
     */
    private fun filter(): IntentFilter {
        val filter = IntentFilter()
        filter.addAction(Constant.RECORD)
        return filter
    }
    private fun getTime(start_time:String,end_time:String ):Int{
        val strhour = ClassCalendar.getHour()
        val hour1 = java.lang.Double.parseDouble(strhour)
        val start_time1 = start_time.substring(0, 2)
        val start_time2 = start_time.substring(3, 4)
        var start_time3 = start_time.substring(4)

        val end_time1 = end_time.substring(0, 2)
        val end_time2 = end_time.substring(3, 4)
        var end_time3 = end_time.substring(4)

        val s = start_time1 + "." + start_time2 + start_time3
        val s1 = end_time1 + "." + end_time2 + end_time3
        val v = java.lang.Double.parseDouble(s)
        val v1 = java.lang.Double.parseDouble(s1)
        Log.e("ATG", "当前时间 $hour1 开始$v  结束$v1")
        return if (hour1>v&&hour1<v1){
            0
        }else if (hour1<v){
            1
        }else {
            2
        }

    }

}